﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.AI;

namespace SLBehaviourSystem
{
    public enum MoveType
    {
        NavMesh,
        CharacterController,
        NotCollision,
    }
    public static class MoveHelper
    {
        public static bool MoveToObj(GameObject movedObj, GameObject targetObj, float speed, float distanceOffset, MoveType type = MoveType.NavMesh)
        {
            return MoveToPos(movedObj, targetObj.transform.position, speed, distanceOffset, type);
        }

        public static bool MoveToPos(GameObject movedObj, Vector3 pos, float speed, float distanceOffset, MoveType type = MoveType.NavMesh)
        {
            float distance = Vector3.Distance(pos, movedObj.transform.position);
            if (distance < distanceOffset)
            {
                return true;
            }
            switch (type)
            {
                case MoveType.NavMesh:
                    NavMeshAgent agent = movedObj.GetComponent<NavMeshAgent>();
                    if (agent != null)
                    {
                        agent.Move((pos - movedObj.transform.position).normalized * speed);
                        distance = Vector3.Distance(pos, movedObj.transform.position);
                    }
                    else
                    {
                        DebugUtils.LogError(movedObj.name + " 上未配置NavMeshAgent");
                    }

                    return distance < distanceOffset;
                case MoveType.CharacterController:
                    CharacterController characterCtrl = movedObj.GetComponent<CharacterController>();
                    if (characterCtrl != null)
                    {
                        characterCtrl.SimpleMove((pos - movedObj.transform.position).normalized * speed);
                        distance = Vector3.Distance(pos, movedObj.transform.position);
                    }
                    else
                    {
                        DebugUtils.LogError(movedObj.name + " 上未配置CharacterController");
                    }
                    return distance < distanceOffset;
                case MoveType.NotCollision:
                    DebugUtils.LogError("请勿使用该模式");
                    return true;
                default:
                    return true;
            }
        }
    }
}
